﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T.FAS.Runtime.Action.Service
{
    /// <summary>
    /// 本地日志服务
    /// </summary>
    static class LocalLogService
    {
        private static object syncObj = new object();

        /// <summary>
        /// 记录异常信息
        /// </summary>
        /// <param name="er">异常信息</param>
        public static void Write(string category, string message)
        {
            if (string.IsNullOrEmpty(message))
                return;
            string errPath = string.Empty;
            try
            {
                string path = GetPath();
                if (Directory.Exists(path) == false)
                    Directory.CreateDirectory(path);
                path = Path.Combine(path, string.Format("FASActionLog-{0}-{1}.txt", category, DateTime.Now.ToString("yyyyMMdd")));
                errPath = path;
                StringBuilder sb = new StringBuilder(100);
                sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "======");
                sb.AppendLine(message);

                File.AppendAllText(path, sb.ToString());

                ManageSampleLogFile();
            }
            catch (System.Exception err)
            {
                if (string.IsNullOrEmpty(errPath) == false)
                {
                    try
                    {
                        StringBuilder sb = new StringBuilder(100);
                        sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "======");
                        sb.AppendLine("Message:" + err.Message);
                        sb.AppendLine("Stack:" + err.ToString());
                        File.AppendAllText(errPath, sb.ToString());
                    }
                    catch
                    { }
                }
            }
        }

        public static void Log(string msg)
        {
            try
            {
                var filename = new StringBuilder();
                filename.Append(@"C:\Temp\FAS\ActionLog_");
                filename.Append(DateTime.Now.ToString("yyyyMMddHHmm"));
                filename.Append(".txt");

                if (!Directory.Exists(@"C:\Temp\FAS\"))
                {
                    Directory.CreateDirectory(@"C:\Temp\FAS\");
                }

               // ManageLogFile();

                using (StreamWriter sw = new StreamWriter(filename.ToString(), true))
                    try
                    {
                        sw.WriteLine(System.DateTime.Now.ToString() + " :" + msg);
                        sw.Flush();
                    }
                    catch { }
                    finally
                    {
                        sw.Close();
                    }
            }
            catch { }
        }
        private static void ManageSampleLogFile()
        {
            string[] Alllogfile = Directory.GetFiles(GetPath());
            foreach (string logfile in Alllogfile)
            {
                if (logfile.Contains("ActionLog"))
                {
                    string filetime = logfile.Substring(logfile.Length - 12, 8);
                    TimeSpan span = System.DateTime.Now - Convert.ToDateTime(new System.DateTime(Convert.ToInt16(filetime.Substring(0, 4)),
                        Convert.ToInt16(filetime.Substring(4, 2)), Convert.ToInt16(filetime.Substring(6, 2)), 0, 0, 0));
                    if (span.Days > 1)
                    {
                        File.Delete(logfile);
                    }
                }
            }
        }

        private static void ManageLogFile()
        {
            string[] Alllogfile = Directory.GetFiles(GetPath());
            foreach (string logfile in Alllogfile)
            {
                if (logfile.Length >= 38)
                {
                    string filetime = logfile.Substring(27, logfile.Length - 31);

                    TimeSpan span = System.DateTime.Now - Convert.ToDateTime(new System.DateTime(Convert.ToInt16(filetime.Substring(0, 4)),
                        Convert.ToInt16(filetime.Substring(4, 2)), Convert.ToInt16(filetime.Substring(6, 2)), Convert.ToInt16(filetime.Substring(8, 2)), Convert.ToInt16(filetime.Substring(10, 2)), 0));
                    if (span.Minutes > 3)
                    {
                        File.Delete(logfile);
                    }
                }
            }
        }

        private static string GetPath()
        {
            return Path.Combine(System.Environment.CurrentDirectory, "FASActionLog");
        }
    }
}
